var $;
layui.use(['laydate','jquery','form'], function() {
	var form = layui.form;
	var	layer = layui.layer;
	$ = layui.jquery;

	var dataId = $(":hidden[name='id']").val();

	$("#uploadify").uploadify({
		auto : true,
		buttonText : "选择文件",
		width : 90,
		height : 30,
		uploader : "/" + $("#uploadUrl").val() + "/upload?ver=" + Math.random() + "&dataId=" + dataId,
		swf : "/static/uploadify/uploadify.swf?ver=" + Math.random(),
		fileTypeExts : "*.png;*.jpg;*.jpeg;*.gif;*.bmp",
		fileTypeDesc : "请选择文件",
		fileSizeLimit : "1GB",
		fileObjName : "uploadify",
		onUploadStart : function(file) {
			$("#uploadify").uploadify("settings", "formData", {
				"dataId" : dataId
			});
		},
		onUploadSuccess : function(file, data, response) {
			var json = $.parseJSON(data);
			var str = '<input type="hidden" name="imgIds" value="' + json.imgId + '" /> ';
			str += '<a href="javascript:void(0);" class="photo" data-id="' +  json.path + '"><img src="' + json.path + '" /></a>';
			if (dataId == null) {
				str += ' <a href="#" class="remove_img">删除</a>';
			} else {
				str += ' <a data-url="/' + $("#uploadUrl").val() + '/delete_img/' + json.imgId + '" class="delFile" style="cursor:pointer">删除</a>';
			}
			$("div#upload").append(str);
			form.render();
		}
	});

	$(".container-fluid").on("click", ".remove_img", function() {
		$(this).prev().prev().remove();
		$(this).prev().remove();
		$(this).remove();
		return false;
	});

	$("#file_upload").on("click", function() {
		$('#uploadify').uploadify('upload', '*');
		return false;
	});

	$("#file_cancel").on("click", function() {
		$('#uploadify').uploadify('cancel', '*');
		return false;
	});

	/*$('.delFile').click(function(){
		var url = $(this).attr('data-url');
		parent.layer.confirm("确定删除吗？", {
			btn : [ "确定", "取消" ]
		}, function() {
			parent.layer.closeAll();
			$.ajax({
				url : encodeURI(url),
				type : 'get',
				dataType : "json",
				cache : false,
				processData : false,
				contentType : false,
				success : function(data) {
					if (data.rs) {
						window.location.reload();
					}
				},
				error : function() {
					layer.msg("服务器内部错误");
				}
			});
		}, function() {

		});
	});*/

	//flash插件判断 
	hasFlash();
	
	
	
});

function hasFlash(){
	var FlashObject = (function(){
		  if(typeof window.ActiveXObject != "undefined"){
		    return new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		  }else{
		    return navigator.plugins['Shockwave Flash'];
		  }
	})();
	
	if (!FlashObject){
		$("#uploadify").on("click", function(){
			event.preventDefault();
			return false;
		});
		$("#uploadify").after("<span><font color='red'>您的浏览器需允许flash运行</font></span>");
	}
}